public with sharing class iFulfillCreateTask{
/** Class Name: iFulfillAssignWorkflow
* Author: Accenture
* Date: 12-Jul-2011 
* Requirement/Project Name: iFulfill 
* Requirement Description: Creates tasks for escalated,outed and live Fulfillment ITems
**/

//static var to prevent recursive trigger updates   
 private static boolean IsRecursive = true;
 
 //Task Recordtype  
 public static ID ifulfillTaskRecordTypeId = RecordTypeHelper.GetRecordTypeID('Task', IfullfillTextConstant.ifulfillTaskRecordType); 
 
 /*Getter method for the recursive*/
 
 public static boolean IsRecursive() {     
       return isRecursive;
 }
 /*
  * setter method for  recursive 
  */          
 public static void setRecursive(boolean value) {     
       isRecursive=value;
 }

 /**
  * This Method Create Task based on the Fulfillment item is Escalated,Status is Outed,Live.
  */
 public static void createTask(list<Fullfilment_Item__c> newFItems,Map<Id,Fullfilment_Item__c> statusCheck){        
    List <Task> taskList =new List<Task>();   
    Task newTask; 
   for(Fullfilment_Item__c FI :newFItems){        
     newTask = new Task();          
     newTask.Priority = 'High';
     newTask.Status = 'Not Started';
     newTask.ReminderDateTime=System.now();
     newTask.ActivityDate=System.today();
     newTask.Type ='Fulfillment';
     newTask.WhatId = FI.ID; 
     newTask.OwnerID=FI.OwnerID;   
     newTask.RecordTypeID=ifulfillTaskRecordTypeId;
    //Escalated      
    if(FI.Escalated__c && FI.Escalated__c !=statusCheck.get(FI.ID).Escalated__c){        
         newTask.Description = IfullfillTextConstant.FI_ESCALATED_TASK_DESC;
         newTask.Subject = IfullfillTextConstant.FI_ESCALATED_TASK_SUBJECT;
         newTask.IsReminderSet = true;       
         taskList.add(newTask);         
    }
    /*Checks if the Fulfillment Status is 'Outed'*/
    else if(((String)FI.Status__c).equalsIgnoreCase(IfullfillTextConstant.FI_WORKFLOW_OUTED_STATUS)&& FI.Status__c !=statusCheck.get(FI.ID).Status__c){          
        newTask.Description = IfullfillTextConstant.FI_OUTED_TASK_DESC;
        newTask.Subject = IfullfillTextConstant.FI_OUTED_TASK_SUBJECT;
        newTask.IsReminderSet = true;
        taskList.add(newTask);          
    }
    /*Checks if the Fulfillment Status is 'Live'*/
    else if(((String)FI.Status__c).equalsIgnoreCase(IfullfillTextConstant.FI_WORKFLOW_LIVE_STATUS) && FI.Status__c !=statusCheck.get(FI.ID).Status__c){  
        newTask.Description = IfullfillTextConstant.FI_LIVE_TASK_DESC ;
        newTask.Subject = IfullfillTextConstant.FI_LIVE_TASK_SUBJECT;
        newTask.IsReminderSet = false;
        taskList.add(newTask);
     }
    }  
    try{insert taskList;  
        setRecursive(false);      
    }catch(DMLException e){GeneralCOnstants.mailAnyExceptionToMe('iFulfillCreateTask','createTask',e);}    
  
} 

/**
  * This Method Create Task when ATTUID of owner will not match to ATTUID of fulfillment Item.
  */

public static void reminderevents (list<Fullfilment_Item__c> newFItems)
{  
List <Task> eventList =new List<Task>(); 
List<Task> taskList = new List<Task>();
Map<String,ID> taskMap = new Map<String,ID>();
String Fulfillment='Fulfillment';
String Normal='Normal';
String status='Not Started';

String taskQry='select WhatID,CallObject from Task where RecordTypeID=\''+ifulfillTaskRecordTypeId +'\' and CallObject != null and type=\''+Fulfillment+'\' and Priority=\''+Normal+'\' and Status=\''+status+'\'';
    
   try{
      taskList= Database.Query(taskQry);  
    }catch(DmlException e){
       GeneralConstants.mailAnyExceptionToMe('iFullCreateTask','reminderevents',e);
    }
    for(Task T:taskList){
      taskMap.put(T.WhatID+'-'+T.CallObject,T.WhatID);
    }
     Task newTask; 
     for(Fullfilment_Item__c FI :newFItems){     
        
     System.debug('Content Version'+FI.Content_Version__c);   
     System.debug('FI ID'+FI.ID);
     if(taskMap.containsKey(FI.ID+'-'+FI.Content_Version__c)){
        break;
     }else{
       newTask= new Task();  
       newTask.Priority = 'Normal';
       newTask.Subject = 'Fulfillment Notice - Content has been updated';
       newTask.Description = 'Online Content has been updated in iFulfill' ;
       newTask.Status = 'Not Started';
       newTask.type='Fulfillment';
       newTask.OwnerID=FI.OwnerID;
       newTask.ActivityDate = System.Today();
       newTask.ReminderDateTime=System.now();
       newTask.IsReminderSet = true;   
       newTask.WhatId = FI.ID;
       newTask.CallObject= String.valueOf(FI.Content_Version__c);
       newTask.RecordTypeID=ifulfillTaskRecordTypeId;    
       System.debug('Task Data'+newTask)  ;
       eventList.add(newTask);
      }   
      try{insert eventList;              
      }catch(DMLException e){GeneralCOnstants.mailAnyExceptionToMe('iFulfillCreateTask','reminderevents',e);} 

      
} }     
}